package com.work.yshop.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.work.yshop.R;
import com.work.yshop.model.CartItem;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车适配器
 */
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {
    
    private List<CartItem> cartItems = new ArrayList<>();
    private OnCartItemClickListener listener;
    
    public void setData(List<CartItem> cartItems) {
        this.cartItems = cartItems;
        notifyDataSetChanged();
    }
    
    @NonNull
    @Override
    public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cart, parent, false);
        return new CartViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull CartViewHolder holder, int position) {
        CartItem item = cartItems.get(position);
        
        // 设置商品信息
        if (item.getProduct() != null) {
            holder.tvName.setText(item.getProduct().getName());
            holder.tvPrice.setText("¥" + item.getProduct().getPrice());
            
            // 加载商品图片
            Glide.with(holder.itemView.getContext())
                    .load(item.getProduct().getImageUrl())
                    .placeholder(R.drawable.placeholder_image)
                    .into(holder.ivProduct);
        }
        
        // 设置数量
        holder.tvQuantity.setText(String.valueOf(item.getQuantity()));
        
        // 设置选中状态
        holder.cbSelect.setChecked(item.isSelected());
        
        // 设置小计金额
        holder.tvSubtotal.setText("¥" + item.getSubtotal());
        
        // 设置点击事件
        holder.cbSelect.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemSelectChanged(position, holder.cbSelect.isChecked());
            }
        });
        
        holder.btnDecrease.setOnClickListener(v -> {
            if (listener != null && item.getQuantity() > 1) {
                listener.onQuantityChanged(position, item.getQuantity() - 1);
            }
        });
        
        holder.btnIncrease.setOnClickListener(v -> {
            if (listener != null) {
                listener.onQuantityChanged(position, item.getQuantity() + 1);
            }
        });
        
        holder.btnDelete.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemDeleted(position);
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return cartItems.size();
    }
    
    public void setOnCartItemClickListener(OnCartItemClickListener listener) {
        this.listener = listener;
    }
    
    /**
     * 购物车项点击监听器
     */
    public interface OnCartItemClickListener {
        void onItemSelectChanged(int position, boolean isSelected);
        void onQuantityChanged(int position, int quantity);
        void onItemDeleted(int position);
    }
    
    /**
     * 购物车ViewHolder
     */
    static class CartViewHolder extends RecyclerView.ViewHolder {
        CheckBox cbSelect;
        ImageView ivProduct;
        TextView tvName;
        TextView tvPrice;
        TextView tvQuantity;
        TextView tvSubtotal;
        View btnDecrease;
        View btnIncrease;
        View btnDelete;
        
        public CartViewHolder(@NonNull View itemView) {
            super(itemView);
            cbSelect = itemView.findViewById(R.id.cb_select);
            ivProduct = itemView.findViewById(R.id.iv_product);
            tvName = itemView.findViewById(R.id.tv_name);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvQuantity = itemView.findViewById(R.id.tv_quantity);
            tvSubtotal = itemView.findViewById(R.id.tv_subtotal);
            btnDecrease = itemView.findViewById(R.id.btn_decrease);
            btnIncrease = itemView.findViewById(R.id.btn_increase);
            btnDelete = itemView.findViewById(R.id.btn_delete);
        }
    }
} 